﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;
using System.Collections;

namespace CSharpLearningWeb
{
    public partial class Tool : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Page_Init(object sender, EventArgs e)
        {
            ParseControls();
            BindInfo();
        }

        private void ParseControls()
        {
            BindDDL();
            //create argument list
            XsltArgumentList xslArg = new XsltArgumentList();
            xslArg.AddParam("employeeId", "", ddlEmployee.SelectedValue);

            //load the data
            XPathDocument xdoc = new XPathDocument(Server.MapPath("Tool.xml"));
            //load Xslt
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(Server.MapPath("Tool.xslt"));
            StringWriter sw = new StringWriter();
            //transform it
            transform.Transform(xdoc, xslArg, sw);
            string result = sw.ToString();

            //remove namespace
            result = result.Replace("xmlns:asp=\"remove\"", "");
            //parse control
            Control ctrl = Page.ParseControl(result);
            phEmployeeAddress.Controls.Add(ctrl);
        }
        private void BindInfo()
        {
            lblMessage.Text = "";

            //find dropdown control and update datasource
            try
            {
                DropDownList ddlCountry1 = phEmployeeAddress.FindControl("ddlCountry1") as DropDownList;
                string selectedValue1 = ddlCountry1.Items[0].Value.Trim();
                ddlCountry1.DataSource = GetCountryList();
                ddlCountry1.DataBind();

                DropDownList ddlCountry2 = phEmployeeAddress.FindControl("ddlCountry2") as DropDownList;
                string selectedValue2 = ddlCountry2.Items[0].Value.Trim();
                ddlCountry2.DataSource = GetCountryList();
                ddlCountry2.DataBind();

                //select the selected value
                ddlCountry1.SelectedValue = selectedValue1;
                ddlCountry2.SelectedValue = selectedValue2;
            }
            catch
            {
                //ex
                throw;
            }
            //find control to add event handler
            /*===============================================================
            Note: Event handler adding process should be after parsing 
             * the controls since controls will be available after parsing
            =================================================================*/
            Button btnSaveAddress = (Button)phEmployeeAddress.FindControl("btnSaveAddress");
            btnSaveAddress.Click += new EventHandler(btnSaveAddress_Click);
        }
        private void BindDDL()
        {
            if (Context.Request.QueryString["id"] != null)
            {
                ddlEmployee.SelectedValue = Context.Request.QueryString["id"];
            }
        }
        protected void btnLoadData_Click(object sender, EventArgs e)
        {
            string qryString = "?id=" + ddlEmployee.SelectedValue;
            Response.Redirect("Default.aspx" + qryString);
        }
        protected void btnSaveAddress_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("Address.xml"));
            XmlNodeList nodeList = doc.SelectNodes("/root/Employee");
            int ind1 = 1;
            int ind2 = 1;
            foreach (XmlNode parentNode in nodeList)
            {
                if (parentNode.Attributes.GetNamedItem("Id").Value.Equals(ddlEmployee.SelectedValue))
                {
                    XmlNode sectionItem = parentNode.SelectSingleNode("Address");
                    foreach (XmlNode node in sectionItem.ChildNodes)
                    {
                        ind2 = 1;
                        foreach (XmlNode subNode in node.ChildNodes)
                        {
                            string ctrlId = subNode.Attributes.GetNamedItem("Caption").Value + "_" + ind1 + ind2;
                            ctrlId = ctrlId.Replace(' ', '_');
                            string ctrlType = subNode.Attributes.GetNamedItem("Type").Value;
                            if (ctrlType.Equals("CountryDDL"))
                            {
                                object ctrl = phEmployeeAddress.FindControl("ddlCountry" + ind1);
                                DropDownList ddlCountry = ctrl as DropDownList;
                                subNode.InnerText = ddlCountry.SelectedValue;
                            }
                            else
                            {
                                object ctrl = phEmployeeAddress.FindControl(ctrlId);
                                TextBox txtbox = ctrl as TextBox;
                                subNode.InnerText = txtbox.Text;
                            }
                            ind2 += 1;
                        }
                        ind1 += 1;
                    }
                }
            }
            doc.Save(Server.MapPath("Address.xml"));
            lblMessage.Text = "Data Saved Successfully";
        }
        private ArrayList GetCountryList()
        {
            ArrayList al = new ArrayList();
            al.Add(new ListItem("Bangladesh", "BD"));
            al.Add(new ListItem("Japan", "JP"));
            al.Add(new ListItem("United States", "USA"));
            return al;
        }

    }
}